<?php
//Copyright (c) 2003-09 PHPWind
!defined('P_W') && exit('Forbidden');

$ceversion = 0;//define('CE','1');
$c_agentip = 1;

if (!get_magic_quotes_gpc()) {
	Add_S($_POST);
	Add_S($_GET);
	Add_S($_COOKIE);
}
Add_S($_FILES);
$pwServer = GetServer(array('HTTP_REFERER','HTTP_HOST','HTTP_X_FORWARDED_FOR','HTTP_USER_AGENT','HTTP_CLIENT_IP','HTTP_SCHEME','HTTPS',
							'PHP_SELF','REQUEST_URI','REQUEST_METHOD','REMOTE_ADDR','QUERY_STRING'));
!$pwServer['PHP_SELF'] && $pwServer['PHP_SELF'] = GetServer('SCRIPT_NAME');

require_once(D_P.'data/bbscache/config.php');

$onlineip = 'Unknown';
if ($db_xforwardip) {
	if ($pwServer['HTTP_X_FORWARDED_FOR'] && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/',$pwServer['HTTP_X_FORWARDED_FOR'])) {
		$onlineip = $pwServer['HTTP_X_FORWARDED_FOR'];
	} elseif ($pwServer['HTTP_CLIENT_IP'] && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/',$pwServer['HTTP_CLIENT_IP'])) {
		$onlineip = $pwServer['HTTP_CLIENT_IP'];
	}
}
if ($onlineip == 'Unknown' && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/',$pwServer['REMOTE_ADDR'])) {
	$onlineip = $pwServer['REMOTE_ADDR'];
	$c_agentip = 0;
}

$db_cvtime!=0 && $timestamp += $db_cvtime*60;
if ($db_forcecharset && !defined('W_P') && !defined('AJAX')) {
	@header("Content-Type:text/html; charset=$db_charset");
}

if (P_W=='global') {
	if ($db_dir && $db_ext) {
		$_NGET = array();
		$self_array = explode('-',substr($pwServer['QUERY_STRING'],0,strpos($pwServer['QUERY_STRING'],$db_ext)));
		$s_count = count($self_array);
		for ($i=0;$i<$s_count-1;$i++) {
			$_key	= $self_array[$i];
			$_value	= rawurldecode($self_array[++$i]);
			$_NGET[$_key] = addslashes($_value);
		}
		!empty($_NGET) && $_GET = $_NGET;
		unset($_NGET);
	}

	if ($db_loadavg && !defined('W_P')) {
		if (@file_exists('/proc/loadavg')) {
			$db_avgstats = GetAvg();
		} elseif (!file_exists(D_P.'data/avgstats.txt') || $timestamp-filemtime(D_P.'data/avgstats.txt')>1800) {
			$db_avgstats = GetAvg();
			writeover(D_P.'data/avgstats.txt',$db_avgstats);
		} else {
			$db_avgstats = readover(D_P.'data/avgstats.txt');
		}
		$db_avgstats>$db_loadavg && $db_cc = 2;
	}
}
if ($db_cc && ((!$_COOKIE && !$pwServer['HTTP_USER_AGENT']) || ($db_cc==2 && $c_agentip))) {
	exit('Forbidden');
}
function GetAvg(){//create by noizy
	if (@file_exists('/proc/loadavg')) {
		if (!($fp = @fopen('/proc/loadavg','r'))) return 0;
		$avgdata = @fread($fp,6);
		@fclose($fp);
		list($loadavg) = explode(' ',$avgdata);
		return trim($loadavg);
	} else {
		return 0;
	}
}
?>